package com.wzy.netty.nio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

/**
 * 描述
 * 创建人    Wzy
 * 创建时间: 2020-05-20  22:11
 */
public class NIOClient {
    public static void main(String[] args) throws IOException {
        //得到一个网络通道
        SocketChannel socketChannel = SocketChannel.open();

        socketChannel.configureBlocking(false);

        // 提供服务器的IP 和端口
        InetSocketAddress inetSocketAddress = new InetSocketAddress("127.0.0.1", 5555);
        if (!socketChannel.connect(inetSocketAddress)) {
            while (!socketChannel.finishConnect()) {
                System.out.println("因为连接需要时间,客户端不会阻塞,可以其他工作...");
            }
        }
        //如果连接成功就发送数据
        String str = "hello,World!";
        ByteBuffer byteBuffer = ByteBuffer.wrap(str.getBytes());
        //发送数据,将buffer写入channel
        socketChannel.write(byteBuffer);
        System.in.read();

    }
}
